package com.Arsanesia.TempleRush.Models;

import com.Arsanesia.TempleRush.Controllers.World;
import com.Arsanesia.TempleRush.Enumeration.Notif;
import com.Arsanesia.TempleRush.Enumeration.TempleRushSound;
import com.Arsanesia.TempleRush.Views.Screens.AbstractScreen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class Notification implements Updateable{

    private Notif type;
    private float notifXTarget;
    private float notifYTarget;
    private float notifX;
    private float notifY;
    private float upperTargetX;
    private float lowerTargetX;
    private float upperNotifLineX,upperNotifLineY;
    private float lowerNotifLineX,lowerNotifLineY;
    private Array<Sprite> notifBgCollection;
    private Array<Sprite> notifTypeCollection; 
    private Array<Sprite> notifLineCollection;
    private Image notifImage;
    private Image notifBg;
    private Image upperNotifLineImage;
    private Image lowerNotifLineImage;
    
    
	public Notification(){
		notifXTarget = AbstractScreen.VIEWPORT_DEFAULTWIDTH;
		notifYTarget = AbstractScreen.VIEWPORT_DEFAULTHEIGHT;
		notifX = 0;
		notifY = notifYTarget;
	}
	public Notification(Notif type){
		this();
		this.type = type;
	}
	public Notification(TextureAtlas atlas){
		this();
		notifTypeCollection = atlas.createSprites("NOTIF");
		notifBgCollection = atlas.createSprites("NOTIFBG");
		notifLineCollection = atlas.createSprites("NOTIFLINE");
		TextureRegion defaultregion = notifTypeCollection.get(Notif.PERFECT.ordinal());
		
		//create notification image
		notifImage = new Image(new TextureRegion(defaultregion));
		notifBg = new Image(new TextureRegion(notifBgCollection.get(Notif.PERFECT.ordinal())));
		Color c = notifBg.getColor();
		c.a = 0 ;//set to transparent
		notifBg.setColor(new Color(c));
		
		//create upper and lower line
		upperNotifLineImage = new Image(new TextureRegion(notifLineCollection.get(0)));
		lowerNotifLineImage = new Image(new TextureRegion(notifLineCollection.get(1)));
		//set up the line position
		upperTargetX = (notifXTarget - upperNotifLineImage.getWidth())/2;
		lowerTargetX =  upperTargetX;
	
		
		
		//set the position to center of screen
		resetNotificationPosition();
		
		Color transparentcolor= notifImage.getColor();
		//change color transparency
		transparentcolor.a=0;
		//make the notification transparent first;
		notifImage.setColor(transparentcolor);
		Gdx.app.log("notif", "notification position: "+notifImage.getWidth()+","+notifImage.getHeight());
	}
	
	private void resetNotificationPosition(){
		//set notification position
		notifX = (notifXTarget - notifImage.getWidth())/2;
		notifY  = (notifYTarget - notifImage.getHeight())/2;
		notifImage.setPosition(notifX, notifY);
		//set notification background position to be in center
		notifBg.setPosition((notifXTarget - notifBg.getWidth())/2, (notifYTarget - notifBg.getHeight())/2);
		//set line position
		upperNotifLineX = -upperNotifLineImage.getWidth();
		lowerNotifLineX = (notifXTarget-upperNotifLineX)/2;
		lowerNotifLineY = (notifYTarget - notifImage.getHeight()/2)/2;
		upperNotifLineY = lowerNotifLineY + lowerNotifLineImage.getHeight();
		upperNotifLineImage.setPosition(upperNotifLineX, upperNotifLineY);
		lowerNotifLineImage.setPosition(lowerNotifLineX, lowerNotifLineY);
	}
	public void resetNotificationActions(boolean renderbg){
		notifImage.addAction(Actions.sequence(Actions.fadeIn(0.1f), Actions.delay(0.45f, Actions.fadeOut( 0.2f))));
		
		if(renderbg)
			notifBg.addAction(Actions.sequence(Actions.fadeIn(0.1f), Actions.delay(0.45f, Actions.fadeOut( 0.2f))));
		
		float upperY = upperNotifLineImage.getY();
		float upperWidth = upperNotifLineImage.getWidth();
		upperNotifLineImage.addAction(Actions.sequence(Actions.moveTo(upperTargetX-100, upperY, 0.3f),
													   Actions.moveTo(upperTargetX, upperY, 0.45f),
													   Actions.moveTo(notifXTarget+upperWidth, upperY, 0.1f)));
		float lowerY = lowerNotifLineImage.getY();
		float lowerWidth = lowerNotifLineImage.getWidth();
		lowerNotifLineImage.addAction(Actions.sequence(Actions.moveTo(lowerTargetX+100, lowerY, 0.3f),
													   Actions.moveTo(lowerTargetX, lowerY, 0.45f),
													   Actions.moveTo(0-lowerWidth, lowerY, 0.1f)));
	}
	public void addNotificationToStage(Stage stage){
		
		stage.addActor(lowerNotifLineImage);
		stage.addActor(upperNotifLineImage);
		stage.addActor(notifBg);
		stage.addActor(notifImage);
	}
	public void setType(Notif type){
		this.type = type;
		boolean renderbg = type == Notif.MISS || type==Notif.PERFECT;
		
		if(type == Notif.MISS)
			GameConfiguration.playSoundEffect(TempleRushSound.valueOf(Notif.MISS.name()));
		//reset action
		resetNotificationActions(renderbg);
		//set the position to center of screen
		resetNotificationPosition();
		//change the image according to the type
		notifImage.setDrawable(new TextureRegionDrawable(notifTypeCollection.get(type.ordinal())));
		if(renderbg)
			notifBg.setDrawable(new TextureRegionDrawable(notifBgCollection.get((type == Notif.MISS)? type.ordinal()-1:type.ordinal())));
	}
	

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

}
